import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-07
 * Time: 23:04
 */
class Solution3 {
    public int calculate(String s) {
        int n = s.length();
        Deque<Integer> stack = new ArrayDeque();
        // 统计数
        int i = 0;
        // 操作符 后续统一
        char op = '+';
        char[] s1 = s.toCharArray();
        while(i < n) {
            if(s1[i] == ' ') {
                i++;
            }else if(s1[i] >= '0' && s1[i] <= '9') {
                int tmp = 0;
                while(i < n && s1[i] >= '0' && s1[i] <= '9') {
                    // 提取数字
                    tmp = 10 * tmp + (s1[i]-'0');
                    i++;
                }
                if(op == '+') {
                    stack.push(tmp);
                }else if(op == '-') {
                    stack.push(-tmp);
                }else if(op == '*'){
                    stack.push(stack.pop() * tmp);
                }else {
                    stack.push(stack.pop() / tmp);
                }
            }else {
                op = s1[i];
                i++;
            }
        }
        int sum = 0;
        // 将栈的数字加起来
        while(!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }
}
